home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
- Subject: Re: Access carry flag from C
- Date: Tue, 20 Feb 96 21:54:32 GMT
- Organization: none
- Message-ID: <824853272snz@genesis.demon.co.uk>
- References: <Dn1C9z.DGv.0.net@indra.com> <ARTHUR.96Feb20143404@gold.Smallworld.co.uk>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <ARTHUR.96Feb20143404@gold.Smallworld.co.uk>
- arthur@Smallworld.co.uk "Arthur Chance" writes:
-
- >In article <31298D20.41C6@bazis.nl> Franz Korntner <fkorntne@bazis.nl> writes:
- >> j+k will overflow when the result exceeds MAXINT
- >>
- >> Thus: "if (j+k > MAXINT) overflow();" but the operation is undefined
- >> if the result overflows, so the expression needs rewriting to make sure
- >> this doesn't happen. The result is then "if (j>MAXINT-k) overflow();".
- >
- >As we must be talking about signed ints, because unsigned can't cause
- >undefined behaviour by overflow, if k < 0, then MAXINT-k overflows.
- >
- >Basically, the C *standard* is useless on things like signed overflow
- >(or word size, or what happens with right shift of -ve numbers, or
- ><insert your favourite "undefined behaviour" gripe here>). You have
- >to look carefully at each *implementation* you use. You can usually
- >find some way to do what you want, but you have to accept it's never,
- >ever going to be standard C.
-
- It certainly can be done portably although not with the efficiency that
- a platform-specific solution is likely to give you.
-
- int j, k;
-
- ...
-
- if ((j >= 0) ? (k > INT_MAX-j) : (k < INT_MIN-j))
- ...
-
- will test if j+k overflows. You have to perform the test before doing the
- calculation since if j+k overflows the result is undefined behaviour
- whatever you do afterwards.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-